import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebServlet("/addStudent")
public class AddStudentServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/addStudent.jsp").forward(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        String nickn = req.getParameter("nickn");
        String ast = req.getParameter("ast");
        String con = req.getParameter("con");
        String imp = req.getParameter("imp");

        String sql = "INSERT INTO classmates (name, nickn, ast, con, imp) VALUES (?, ?, ?, ?, ?)";
        int result = JdbcUtil.executeUpdate(sql, name, nickn, ast, con, imp);

        if (result > 0) {
            resp.sendRedirect(req.getContextPath() + "/stu");
        } else {
            req.setAttribute("msg", "添加同学信息失败");
            req.getRequestDispatcher("/addStudent.jsp").forward(req, resp);
        }
    }
}